//
//  MBSpaceSetTimerViewController.m
//  MiBaoTu
//
//  Created by admin on 16/9/26.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpaceSetTimerViewController.h"

#import "MBSpaceSetTimerCell.h"

@interface MBSpaceSetTimerViewController ()<UITableViewDataSource,UITableViewDelegate,MBSpaceSetTimerCellDelegate> {
    UITableView                     *_tableView;
    NSMutableArray                  *_mutArr;
    NSMutableArray                  *_timerArr;
    NSString                        *_timerStr;
}

@end

@implementation MBSpaceSetTimerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _mutArr = [[NSMutableArray alloc]initWithObjects:@"10分钟",@"20分钟",@"30分钟",@"60分钟",@"90分钟", nil];
    _timerArr = [[NSMutableArray alloc]init];
    
    _timerStr = [[NSUserDefaults standardUserDefaults]objectForKey:@"TIMER_SET_MUSIC_STOP"];
    if ([_timerStr isKindOfClass:[NSString class]]) {
        if (_timerStr.length > 0) {
            [_timerArr addObject:_timerStr];
        }
    }
    [self setTitleView:@"定时关闭"];
    [self setBackItem];

    [self onCreateUI];
}

#pragma mark - UI
- (void)onCreateUI
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.backgroundView = nil;
    [self.view addSubview:_tableView];
    _tableView.tableFooterView = [self addTableFootView];
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.view.height);
    _tableView.tableFooterView = [self addTableFootView];
    [_tableView reloadData];
}

- (UIView *)addTableFootView
{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200*PERCEN)];
    if (ISIPAD) {
        bgView.frame=CGRectMake(0, 0, SCREEN_WIDTH, 300*PERCEN);
    }
    bgView.backgroundColor = [UIColor clearColor];
    
    UIButton *_loginBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _loginBt.frame = CGRectMake(0, 80*PERCEN, bgView.width, 80*PERCEN);
    if (ISIPAD) {
        _loginBt.frame = CGRectMake((bgView.width-700*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.))/2., 200*PERCEN, 700*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.), 100*PERCEN);
    }
    _loginBt.backgroundColor = TINK_COLOR;
    [_loginBt setTitle:@"确认" forState:UIControlStateNormal];
    [_loginBt setTitle:@"确认" forState:UIControlStateHighlighted];
    [_loginBt setTitleColor:UIColorFromHex(0xffffff) forState:UIControlStateNormal];
    [_loginBt setTitleColor:UIColorFromHex(0xffffff) forState:UIControlStateHighlighted];
    _loginBt.titleLabel.font = [UIFont systemFontOfSize:14.f];
    _loginBt.layer.cornerRadius = 5.f;
    _loginBt.layer.masksToBounds = YES;
    [_loginBt addTarget:self action:@selector(onSureAction) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:_loginBt];
    bgView.userInteractionEnabled=YES;
    return bgView;
}

//确认
- (void)onSureAction
{
    NSLog(@"点击");
    if (_timerArr.count != 0) {
        NSString *showTime = [_timerArr lastObject];
    
        [[NSUserDefaults standardUserDefaults]setObject:showTime forKey:@"TIMER_SET_MUSIC_STOP"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        showTime = [showTime substringWithRange:NSMakeRange(0, showTime.length-2)];
        
        [UserManager standerDefult].timerLength = [showTime intValue] * 60;
        [[UserManager standerDefult] setUpTimer];
    }
    
    [self back];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 勾选
- (void)onSelectTimer:(NSString *)timerString withView:(MBSpaceSetTimerCell *)view
{
    if (_timerArr.count == 0) {
        [_timerArr addObject:timerString];
        [view layoutSelectAction:YES];
        _timerStr = timerString;
    } else {
        if ([_timerArr containsObject:timerString]) {
            [_timerArr removeAllObjects];
            [view layoutSelectAction:NO];
            
            _timerStr = @"";
            [[NSUserDefaults standardUserDefaults]setObject:@"" forKey:@"TIMER_SET_MUSIC_STOP"];
            [[NSUserDefaults standardUserDefaults]synchronize];
        } else {
            NSString *lastString = [_timerArr lastObject];
            if ([_mutArr containsObject:lastString]) {
                NSInteger row = [_mutArr indexOfObject:lastString];
                MBSpaceSetTimerCell *lastCell = (MBSpaceSetTimerCell *)[_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];
                [lastCell layoutSelectAction:NO];
                [view layoutSelectAction:YES];
                [_timerArr removeAllObjects];
                [_timerArr addObject:timerString];
                _timerStr = timerString;
                [_tableView reloadData];
            }
        }
    }
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _mutArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //歌曲
    static NSString *cellID = @"space_setTime_Cell";
    MBSpaceSetTimerCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[MBSpaceSetTimerCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.delegate = self;
    }
    cell.orientation = self.orientation;
    if (_mutArr.count > indexPath.row) {
        cell.timerString = [_mutArr objectAtIndex:indexPath.row];
        if ([cell.timerString isEqualToString:_timerStr]) {
            [cell layoutSelectAction:YES];
        }
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100*PERCEN;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MBSpaceSetTimerCell *cell = (MBSpaceSetTimerCell *)[tableView cellForRowAtIndexPath:indexPath];
    if ([cell isKindOfClass:[MBSpaceSetTimerCell class]]) {
        if (_mutArr.count > indexPath.row) {
            NSString *title = [_mutArr objectAtIndex:indexPath.row];
            [self onSelectTimer:title withView:cell];
        }
    }
}

@end
